// pages/orderCenter/comments.js
import commonUtils from '../../utils/util.js'
import imageUtils from '../../utils/imageUtils'
import requestUrls from '../../requestUrl.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    needsList: [
      '有人跟车', '需回单', '需雨布', '禁止拼车'
    ],
    needs: '',
    showTimeSelect: false,
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    goodsImgList: [],
    showLoading:false,
  },
  changeNeedsType(event) {
    const needs = event.currentTarget.dataset.needstype;
    this.setData({
      needs,
    })
  },
  getTimeSelect() {
    this.setData({
      showTimeSelect: true,
    })
  },
  onCancel() {
    this.setData({
      showTimeSelect: false,
    })
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail
    });
  },
  onConfirm(event) {
    console.log(event);
    const loadingEndTime = new Date(event.detail);
    this.setData({
      loadingEndTime:commonUtils.formatYMDH(loadingEndTime),
      loadingEndTimeStr: commonUtils.formatDate(new Date(event.detail), 'yyyy-MM-dd hh'),
    })
    this.onCancel();
  },
  afterImageRead(event) {
    const { file } = event.detail;
    let uploadUrl = requestUrls.baseUrl + requestUrls.orderTruckImage.uploadImage+"/ORDER_CENTER";
    if(file.length>0){
      this.setData({showLoading: true});
      file.forEach((item,index)=>{
        let filePath = item.path;
        imageUtils.uploadImage(uploadUrl,filePath,(url)=>{
          this.setData({
            goodsImgList:[...this.data.goodsImgList,{url: requestUrls.baseImagePrefixUrl + url, imageUrl: url}],
            showLoading:!index===file.length
          })
        });
      })
    }
  },
  deleteImage(event){
    let imageList = this.data.goodsImgList;
    imageList.splice(event.detail.index,1);
    this.setData({
      goodsImgList:imageList,
    })
  },
  submitComments() {
    let comments = {
      needs: this.data.needs,
      loadingEndTime: this.data.loadingEndTime,
      loadingEndTimeStr:this.data.loadingEndTimeStr,
      goodsImgList: this.data.goodsImgList,
    }
    wx.setStorageSync('comments', comments);
    wx.navigateTo({
      url: '/pages/orderCenter/addv2',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})